<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info sysbusinessPrice">
      <div class="base-content-operate">
        <el-button
          type="primary"
          icon="el-icon-setting"
          @click.native="beatchUploadPrice()"
          >设置价格</el-button
        >

        <el-button
          icon="el-icon-document-copy"
          type="primary"
          @click="exportData()"
          >导出</el-button
        >
        <el-button
          icon="el-icon-document-copy"
          type="primary"
          @click="importData()"
          >导入</el-button
        >
      </div>
      <el-table
        ref="table"
        class="filter unsetborder"
        :class="{ 'show-filter': showFilterRow }"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55">
          <el-table-column type="selection"></el-table-column>
        </el-table-column>
        <el-table-column align="center" width="50">
          <template #header>
            <div class="filter-icon" title="筛选">
              <SvgIcon
                class-name="filter"
                icon-class="filter"
                @click="filterIconClick"
              />
            </div>
          </template>
          <el-table-column width="50"></el-table-column>
        </el-table-column>
        <el-table-column align="center" label="序号" width="50">
          <el-table-column align="center">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column v-if="false" label="ID">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column label="项目名称">
          <el-table-column prop="detectionItemName">
            <template #header>
              <el-input
                v-if="showFilterRow"
                v-model="queryForm.detectionItemName"
                @keyup.enter.native="onQuery"
              ></el-input>
            </template>
            <template slot-scope="scope">
              {{ scope.row.detectionItemName }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="方法标准">
          <el-table-column prop="methodStandardName">
            <template #header>
              <el-input
                v-if="showFilterRow"
                v-model="queryForm.methodStandardName"
                @keyup.enter.native="onQuery"
              ></el-input>
            </template>
            <template slot-scope="scope">
              {{ scope.row.methodStandardName }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="单价(元)">
          <el-table-column>
            <template slot-scope="scope">
              {{ scope.row.agreementPrice }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="协议价1(元)">
          <el-table-column>
            <template slot-scope="scope">
              {{ scope.row.firstAgreementPrice }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="协议价2(元)">
          <el-table-column>
            <template slot-scope="scope">
              {{ scope.row.secondAgreementPrice }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="协议价3(元)">
          <el-table-column>
            <template slot-scope="scope">
              {{ scope.row.thirdAgreementPrice }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="备注/说明">
          <el-table-column>
            <template slot-scope="scope">
              {{ scope.row.explain }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="操作">
          <el-table-column width="140">
            <template #header>
              <div v-if="showFilterRow" class="search-operate">
                <el-link type="primary" @click="onQuery">搜索</el-link>
                <el-link @click="cancelForm">清空</el-link>
              </div>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
      <BasePagination
        :total="pagination.total"
        :page-index="pagination.pageIndex"
        :page-size="pagination.pageSize"
        @sizeChange="sizeChange"
        @changePage="currentChange"
      />
      <DetectionItemBeatchUpdateDialog
        v-if="dialogVisible"
        :dialog-visible.sync="dialogVisible"
        :top-text="topText"
        :detection-item-name-array="detectionItemNameArray"
        @DetectionItemClose="DetectionItemClose"
        @getList="getList"
      />
    </div>
    <ImportComponents
      ref="ImportComponents"
      v-model="importVisible"
      @success="closeDialog"
      @UploadMethod="UploadMethod"
      @DownloadMethod="DownloadMethod"
    />
  </div>
</template>
<script>
import {
  GetDetectionItemPriceList,
  UpdatePrice,
} from "../../../api/SysBusiness/SysBusinessDetectionItem.js";
import DetectionItemBeatchUpdateDialog from "./DetectionItemBeatchUpdateDialog.vue";
import {
  ImportEntrustUrl,
  DownloadEntrustOrderTemplate,
} from "../../../api/SysBusiness/SysBusinessDetectionItem.js";
import { GetStaticFileInfo } from "@/api/Base";

export default {
  name: "SysBusinessDetectionItemPriceList",
  components: { DetectionItemBeatchUpdateDialog },
  data() {
    return {
      showFilterRow: false,
      importVisible: false,
      dialogVisible: false,
      loading: true,
      topText: "",
      idSendArry: [],
      list: [],
      queryForm: {
        num: "",
        detectionItemName: "",
        methodStandardName: "",
      },
      formData: {
        id: 0,
        parentId: 0,
        productStandardId: 0,
        sysBusinessSampleCategoryId: 0,
        num: "",
        detectionItemName: "",
        anotherName: "",
        detectionObject: "",
        parameterName: "",
        standardMethod: "",
        detectionMethod: "",
        restrictRange: "",
        explain: "",
        detectionItemType: "",
        restrict: "",
        firstAgreementPrice: 0,
        secondAgreementPrice: 0,
        thirdAgreementPrice: 0,
        unitSymbol: "",
        decisionSymbol: "",
        qualityIndex: "",
        methodStandard: "",
        methodStandardName: "",
        AgreementPrice: "",
        AgreementPriceDifferentiate: "",
      },
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
      detectionItemNameArray: [],
      multipleSelection: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onQuery() {
      this.pagination.pageIndex = 1;
      this.getList('filter');
    },

    filterIconClick() {
      this.showFilterRow = !this.showFilterRow;
      !this.showFilterRow && this.cancelForm();
    },
    // 下载
    DownloadMethod() {
      DownloadEntrustOrderTemplate().then((res) => {
        if (res.code === 200) {
          console.log(res)
          GetStaticFileInfo(res.data)
          // const link = document.createElement("a");
          // link.style.display = "none"; // 去除a标签的样式
          // // 设置连接
          // link.href = `${this.$baseProtocol}//${res.data}`;
          // document.body.appendChild(link);
          // 模拟点击事件;
          // link.click();
        } else {
          this.$message.error("未找到模板,请联系管理员");
        }
      });
    },
    // 上传
    UploadMethod(formData, fileCount) {
      ImportEntrustUrl(formData).then((res) => {
        if (res.code === 200) {
          this.count = this.count + 1;
          if (this.count === fileCount) {
            // 调用上传成功方法
            this.$refs.ImportComponents.successfulOperation();
          }
        } else {
          this.count = 0;
          this.$message.error(res.msg);
        }
      });
    },

    // 关闭弹框清空id
    closeDialog() {
      this.getList();
    },
    // 导入
    importData() {
      this.importVisible = true;
    },
    // 清空筛选条件
    cancelForm() {
      this.queryForm = this.$options.data().queryForm;
      this.getList("filter");
    },
    currentChange(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    getList() {
      this.loading = true;
      GetDetectionItemPriceList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.list.forEach((item) => {
            item.firstAgreementPrice = item.firstAgreementPrice / 100;
            item.secondAgreementPrice = item.secondAgreementPrice / 100;
            item.thirdAgreementPrice = item.thirdAgreementPrice / 100;
            item.agreementPrice = item.agreementPrice / 100;
          });
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    beatchUploadPrice(data) {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请选择最少一个检测项目");
        return;
      }
      if (this.multipleSelection.length > 0) {
        this.detectionItemNameArray = [];
        this.multipleSelection.forEach((item) => {
          this.detectionItemNameArray.push(item.detectionItemName);
        });
        this.dialogVisible = true;
      } else {
        this.$message.warning("请选择检测项目");
      }
    },
    outPutQuery() { },

    DetectionItemClose(data) { },
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) =>
        filterVal.map((j) => {
          return v[j];
        })
      );
    },
    // 导出
    exportData() {
      if (
        this.multipleSelection === null ||
        this.multipleSelection.length === 0
      ) {
        this.$message.error("请选择数据");
        return false;
      }
      var tableData = this.multipleSelection;
      import("@/vendor/Export2Excel").then((excel) => {
        const tHeader = [
          "项目名称",
          "方法标准",
          "单价(元)",
          "协议价1(元)",
          "协议价2(元)",
          "协议价3(元)",
          "备注/说明",
        ];
        const filterVal = [
          "detectionItemName",
          "methodStandardName",
          "agreementPrice",
          "firstAgreementPrice",
          "secondAgreementPrice",
          "thirdAgreementPrice",
          "explain",
        ];
        const data = this.formatJson(filterVal, tableData);
        excel.export_json_to_excel({
          header: tHeader, // 表头
          data, // 数据
          filename: "检测项目报价", // 导出文件名
          autoWidth: true, // 导出宽度自动
          bookType: "xlsx", // 导出后缀
        });
      });
    },
    updatePriceFunction(data) {
      var _formData = this.formData;
      _formData.id = data.id;
      _formData.firstAgreementPrice = data.firstAgreementPrice;
      _formData.secondAgreementPrice = data.secondAgreementPrice;
      _formData.thirdAgreementPrice = data.thirdAgreementPrice;
      if (
        _formData.firstAgreementPrice === "" ||
        _formData.secondAgreementPrice === "" ||
        _formData.thirdAgreementPrice === ""
      ) {
        this.$message.warning("价格不能为空");
        return;
      }
      this.$confirm("此操作将设置服务金额, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        callback: (action) => {
          if (action === "confirm") {
            UpdatePrice(_formData)
              .then((res) => {
                this.getList();
              })
              .catch((res) => { });
          }
        },
      });
    },
  },
};
</script>
<style  lang="scss">
.sysbusinessPrice {
  .is-scrolling-none {
    overflow: auto !important;
  }
}
</style>
